package com.xabber.myapplication;


import android.Manifest;
import android.app.Activity;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.media.MediaCodec;
import android.media.MediaExtractor;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import androidx.annotation.RequiresApi;

import java.io.File;
import java.io.IOException;

public class MainActivity extends Activity implements View.OnClickListener {
    private Button start, stop, play;
    private PermissionHelper permissionHelper;
    private boolean mIsPalying = true;
    String s;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        start = findViewById(R.id.start);
        stop = findViewById(R.id.stop);
        play = findViewById(R.id.play);
        start.setOnClickListener(this);
        stop.setOnClickListener(this);
        play.setOnClickListener(this);
        permissionHelper = new PermissionHelper(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.RECORD_AUDIO}, 100);
        permissionHelper.request(new PermissionHelper.PermissionCallback() {
            //这个方法是全都授权后将要进行的操作
            @Override
            public void onPermissionGranted() {

            }

            //某个授权
            @Override
            public void onIndividualPermissionGranted(String[] grantedPermission) {

            }

            //某个拒绝，我们弹出提示
            @Override
            public void onPermissionDenied() {

            }

            //用户选择了"不再询问"后，点击"拒绝按钮"，执行此方法
            @Override
            public void onPermissionDeniedBySystem() {

            }
        });
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.start:
                AudioRecordManager.getInstance(this).startRecord();
                break;
            case R.id.stop:

                s = AudioRecordManager.getInstance(this).stopRecord();


                break;
            case R.id.play:

                File file = new File(s);
                Log.e("fdfssdf", "" + file.length());
                MediaPlayer mediaPlayer = new MediaPlayer();
                try {
                    mediaPlayer.setDataSource(s);
                    mediaPlayer.prepare();
                    mediaPlayer.start();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
        }

    }

}
